﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloMultas
{
    public class conjuntoMultas:conjuntoBase
    {
        #region "constructores"

        public conjuntoMultas(datosAplicacion dA)
            : base(dA)
        {
            this.numTipo = CONJMULTA;
        }

        #endregion

        #region "metodos"

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        #region "imec"

        public void agregarmulta(multa nuevomulta)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_multa(" + nuevomulta.monto + ", '" + nuevomulta.ejemplar + "', '" + nuevomulta.usuario + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar multa");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarmulta(String idUsuario, String idEjemplar)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_multa('" + idEjemplar + "', '" + idUsuario + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar multa");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public multa obtenermulta(String idUsuario, String idEjemplar)
        {
            multa nuevo = null;
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_multa_id('" + idEjemplar + "', '" + idUsuario + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nuevo = new multa(int.Parse(valores[0]), valores[1], valores[2]);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el multa");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nuevo;
        }

        public LinkedList<multa> obtenermultaUsuario(String idUsuario)
        {
            LinkedList<multa> nuevo = new LinkedList<multa>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_multa_Usuario('" + idUsuario + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nuevo.AddLast(new multa(int.Parse(valores[0]), valores[1], valores[2]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el multa");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nuevo;
        }


        public LinkedList<multa> obtenermultas()
        {
            LinkedList<multa> nuevo = new LinkedList<multa>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_multa()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nuevo.AddLast(new multa(int.Parse(valores[0]), valores[1], valores[2]));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener el multa");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nuevo;
        }

        #endregion

        #endregion
    }
}
